/*
 * PlaneSimulatorInputHandler.java
 * 
 * Created on 2008-04-17, 15:02:22
 */

package planesimulator.game;

import com.jme.input.InputHandler;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.input.action.KeyNodeBackwardAction;
import com.jme.input.action.KeyNodeForwardAction;
import com.jme.input.action.KeyNodeRotateLeftAction;
import com.jme.input.action.KeyNodeRotateRightAction;
import com.jme.scene.Spatial;

/**
 * Input Handler of the Plane Simulator game
 * 
 * @author Mateusz 'm4linka' Malinowski (m4linka@gmail.com)
 */
public class PlaneSimulatorInputHandler extends InputHandler {
  
  
  /**
   * Supply the node to control 
   * 
   * @param node the node we wish to move 
   * @param api the library that will handle creation of the input.
   */
  public PlaneSimulatorInputHandler(Spatial node, String api) {
    super();
    setKeyBindings(api);
    setActions(node);
  }
  
  
  /**
   * Creates the keyboard objects
   * 
   * @param api
   */
  private void setKeyBindings(final String api) {
    final KeyBindingManager keyboard = KeyBindingManager.getKeyBindingManager();

    keyboard.set("forward", KeyInput.KEY_W);
    keyboard.set("backward", KeyInput.KEY_S);
    keyboard.set("turnRight", KeyInput.KEY_D);
    keyboard.set("turnLeft", KeyInput.KEY_A);
  }
  
  
  /**
   * Assigns action classes to triggers
   * 
   * @param node
   */
  private void setActions(final Spatial node) {
    final KeyNodeForwardAction forward = new KeyNodeForwardAction(node, 30f);
    addAction(forward, "forward", true);

    final KeyNodeBackwardAction backward = new KeyNodeBackwardAction(node, 15f);
    addAction(backward, "backward", true);

    final KeyNodeRotateRightAction rotateRight = 
        new KeyNodeRotateRightAction(node, 5f);
    rotateRight.setLockAxis(node.getLocalRotation().getRotationColumn(1));
    addAction(rotateRight, "turnRight", true);

    final KeyNodeRotateLeftAction rotateLeft = new KeyNodeRotateLeftAction(node, 5f);
    rotateLeft.setLockAxis(node.getLocalRotation().getRotationColumn(1));
    addAction(rotateLeft, "turnLeft", true);
  }
  
}
